package cn.electronic.sale.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.SessionScope;
import org.springframework.web.multipart.MultipartFile;

import cn.electronic.sale.pojo.Admin;
import cn.electronic.sale.pojo.Goods;
import cn.electronic.sale.pojo.GoodsCustom;
import cn.electronic.sale.pojo.Ordersdetail;

/**
 * 类描述：管理员控制类
 * 
 * @author: nightingale
 * @date： 日期：2016-11-18 时间：上午11:28:08
 * @version 1.0
 */
@Controller
@RequestMapping("/admin")
//为了对url进行分类管理 ，可以在这里定义根路径，最终访问url是根路径+子路径
//比如：/admin/findAdmin.action 
public class AdminController extends BasicController{
	/**
	 * 管理员删除后台订单
	 * 
	 * @param 
	 * @return 返回页面
	 * 
	 * @throws Exception
	 */
	@RequestMapping("deleteOrderDetail")
	public String deleteOrderDetail(
			@RequestParam(value = "id", required = true) Integer detailId)
			throws Exception {
		System.out.println(detailId);
		// 传参然后删除数据
		ordersDetailService.deleteByPrimaryKey(detailId);
		return "redirect:/admin/orderShow.action";
	}
	//redirect:/admin/orderShow.action/   customer/index
	
	/**
	 * 查看订单详情
	 * 
	 * @param 
	 * @return 返回页面
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/orderShowDetail")
	public String orderShowDetail(HttpSession session,@RequestParam(value = "id", required=true) Integer customerId) throws Exception
	{
		//Ordersdetail ordersdetail = (Ordersdetail) session.getAttribute("ordersdetails");
		
		
		//得到了所有订购的商品
		List<GoodsCustom> orderGoodsCustoms = ordersService.selectAllGoodsInfoByCustomerId(customerId);
	session.setAttribute("orderGoodsCustoms", orderGoodsCustoms);
		return "admin/ordersDetailShow";
	}
	
	
	
	/**
	 * 查看订单页面
	 * 
	 * @param 
	 * @return 返回页面
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/orderShow")
	public String orderShow(HttpSession session) throws Exception{
		List<Ordersdetail> ordersdetails = ordersDetailService.selectAllDetail();
		for (Ordersdetail ordersdetail : ordersdetails) {
			ordersdetail.getCustomerId();
		}
		session.setAttribute("ordersdetails", ordersdetails);
		
		
		System.out.println(ordersdetails);
		
		
		return "admin/ordersShow";
	}
	
	/**
	 * 验证管理员登陆
	 * 
	 * @param admin传递过来的表单数据封装在admin对象中
	 * @return 查询表中数据并且输出
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/adminLogin")
	public  String findAdmin(Admin admin , HttpSession session) throws Exception
	{
		Admin adminTest=adminService.findAdminByNameAndPassword(admin);
		System.out.println(adminTest);
		if (adminTest!=null) {
			session.setAttribute("adminTest", adminTest);
			List<GoodsCustom> goodsSearch= goodsService.selectAllGoodsInfo();
			System.out.println(goodsSearch);
			session.setAttribute("goodsSearch", goodsSearch);
		}
		return "admin/index";//后台jsp
	}
	
	/**
	 * 管理员删除已有商品
	 * 
	 * @param goodsId商品ID
	 * @return 返回页面
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/deleteGoods")
	public String deleteGoods(@RequestParam(value="id",required=true) Integer goodsId,HttpSession session) throws Exception
	{
		System.out.println(goodsId);
		
		goodsUploadService.deleteGoodsUploadInfoByGoodsId(goodsId);
		
		goodsService.deleteGoodsByPrimaryKey(goodsId);
		List<GoodsCustom> goodsSearch= goodsService.selectAllGoodsInfo();
		session.setAttribute("goodsSearch", goodsSearch);
		
		return "admin/index";
	}
	
	/**
	 * 管理员跳转到修改商品页面并显示要修改的商品
	 * 
	 * @param goodsId商品ID
	 * @return 返回页面
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/updateGoodsPage")
	public String updateGoodsInfo(HttpSession session,@RequestParam(value="id",required=true) Integer goodsId)throws Exception
	{	
		GoodsCustom goodsCustom=goodsService.selectAllGoodsInfoByPrimaryKey(goodsId);
		session.setAttribute("goodsCustom", goodsCustom);
		return "admin/goodsInfoEdit";
		
		
	}
	/**
	 * 管理员从修改商品页面更新商品
	 * 
	 * @param 
	 * @return 返回页面admin/index页面
	 * 
	 * @throws Exception
	 */
	@RequestMapping("updateGoods")
	public String updateGoods (Goods goods, HttpSession session,
			MultipartFile updateName, HttpServletRequest request)throws Exception
	{
		//debug看看goods里面有没有数据
		
		//获取要修改商品的id
		
		//updateGoodsByPrimaryKey
		adminService.updateGoods(goods);
		
		List<GoodsCustom> goodsSearch= goodsService.selectAllGoodsInfo();
		System.out.println(goodsSearch);
		session.setAttribute("goodsSearch", goodsSearch);
		//获取图像，修改图片名
		
		//更新日期
		
		//updateGoodsInfoByPrimaryKey
		return "admin/index";
	}
	
}
